package com.tanhua.dubbo.api;

import com.tanhua.domain.mongo.Comment;

import com.tanhua.domain.mongo.Movement;

import com.tanhua.domain.mongo.Video;

import com.tanhua.domain.vo.PageResult;

import com.tanhua.dubbo.api.mongo.CommentApi;

import org.apache.dubbo.config.annotation.DubboService;

import org.bson.types.ObjectId;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.domain.Sort;

import org.springframework.data.mongodb.core.MongoTemplate;

import org.springframework.data.mongodb.core.query.Criteria;

import org.springframework.data.mongodb.core.query.Query;

import org.springframework.data.mongodb.core.query.Update;

import java.util.Date;

import java.util.List;

@DubboService

public class CommentApiImpl implements CommentApi {

    @Autowired
    private MongoTemplate mongoTemplate;

//    这个方法是一个通用的保存方法：保存圈子的点赞、评论、喜欢  保存小视频的点赞、评论 保存评论的点赞

    @Override
    public Integer save(Comment comment) {

//        comment.setId();  //主键 ObjectId

//        comment.setCreated(); // new Date()

//        comment.setPublishUserId(); // 对谁发布的圈子点赞

        ObjectId publishId = comment.getPublishId(); //publishId 有可能是圈子的ID  小视频的id 评论的id

        comment.setId(new ObjectId());
        comment.setCreated(new Date().getTime());
        Integer commentType = comment.getCommentType();

        if(commentType==1||commentType==2||commentType==3){

//            对圈子交互：点赞、评论、喜欢

//            update movement_detail set likeCount = likeCount+1 where id=???

            Query query = new Query(Criteria.where("id").is(publishId));

            Update update = new Update();

//            update.set("likeCount",movement.getLikeCount()+1);

            update.inc(comment.getCol(),1);

//            1、圈子的点赞数+1

            mongoTemplate.updateFirst(query,update,Movement.class);

//            2、保存comment   2、圈子的点赞数+1

            Movement movement = mongoTemplate.findById(publishId, Movement.class);
            comment.setPublishUserId( movement.getUserId());

            mongoTemplate.save(comment);

//            3、返回圈子的点赞数

            return  comment.getCount(movement);



        }else if(commentType==4||commentType==5){

//            对小视频交互：点赞、评论

            Query query = new Query(Criteria.where("id").is(publishId));
            Update update = new Update();
            update.inc(comment.getCol(),1);
            mongoTemplate.updateFirst(query, update, Video.class);
            Video video = mongoTemplate.findById(publishId, Video.class);
            comment.setPublishUserId(video.getUserId());
            mongoTemplate.save(comment);
            return comment.getCount(video);
        }else{ //commentType==6

//            对评论交互：点赞

        }



        return null;
    }

//    这个方法是一个通用的保存方法：删除圈子的点赞、评论、喜欢  保存小视频的点赞、评论 保存评论的点赞

    @Override
    public Integer delete(Comment comment) {
        //       delete comment  圈子点赞数-1  返回点赞数

//        当前登录人对XX圈子的点赞

//        delete from comment where userId=?? and pulishId=?? and commentType=1

        Query query = new Query(Criteria.where("userId").is(comment.getUserId()).and("publishId").is(comment.getPublishId()).and("commentType").is(comment.getCommentType()));
        mongoTemplate.remove(query,Comment.class);
        Integer commentType = comment.getCommentType();

        if(commentType==1||commentType==2||commentType==3){

            Query query1 = new Query(Criteria.where("id").is(comment.getPublishId()));
            Update update = new Update();

//            update.set("likeCount",movement.getLikeCount()+1);

            update.inc(comment.getCol(),-1);

//            1、圈子的点赞数-1

            mongoTemplate.updateFirst(query1,update,Movement.class);
            Movement movement = mongoTemplate.findById(comment.getPublishId(), Movement.class);

            return comment.getCount(movement);

        }else if(commentType==4||commentType==5){

//            对小视频交互：点赞、评论

        }else{ //commentType==6

//            对评论交互：点赞

        }



        return null;
    }

    @Override
    public PageResult queryComments(Integer page, Integer pagesize, String movementId,Integer commentType) {

//        select * from comment where publishId=?? and commentType=2  limit start,size

        Query query = new Query(Criteria.where("publishId").is(new ObjectId(movementId)).and("commentType").is(commentType));
        long count = mongoTemplate.count(query, Comment.class);
        query.limit(pagesize).skip((page-1)*pagesize).with(Sort.by(Sort.Direction.DESC,"created"));
        List<Comment> commentList = mongoTemplate.find(query, Comment.class);
        return new PageResult(page,pagesize,(int)count,commentList);
    }

    @Override
    public PageResult queryLikesOrCommentsOrLoves(Long userId,Integer page, Integer pagesize, int commentType) {
        Query query = new Query(Criteria.where("publishUserId").is(userId).and("commentType").is(commentType));
        long count = mongoTemplate.count(query, Comment.class);
        query.limit(pagesize).skip((page-1)*pagesize).with(Sort.by(Sort.Direction.DESC,"created"));
        List<Comment> commentList = mongoTemplate.find(query, Comment.class);
        return new PageResult(page,pagesize,(int)count,commentList);
    }

}
